<?php
/**
 *  地址管理
*/

namespace app\api\service;
use app\api\model\Address as AddressModel;
use app\api\model\RegionCity;
use app\api\model\RegionCountry;
use app\facade\Basic;

class Address
{

    /**
     *  地址列表
    */
    public function list($param)
    {
        $where['type'] = $param['type'];
        $where['user_id'] = $param['user_id'];
        $result = AddressModel::where($where)
            ->field('id,first_name,last_name,email,type,phone,address,is_default,country_id,city_id,country_name,city_name,zip_code,state,state_id,company,address2,remark')
            ->order('is_default DESC')
            ->paginate($param['listRows']);
        return Basic::paginator(0,'common.success',$result);
    }

    /**
     *
    */
    public function getList($param)
    {
        $where['user_id'] = $param['user_id'];
        $where['type'] = $param['type'];
        $result = [];
        $result = AddressModel::where($where)
            ->field('id,first_name,last_name,email,phone,address,type,is_default,country_id,city_id,country_name,city_name,zip_code,state,state_id,company,address2,remark')
            ->order('is_default DESC')
            ->select();

        if ( $result ) {
            $result = $result->toArray();
        }

        return Basic::data(0,'common.success',$result);
    }


    /**
     *  获取默认地址
    */
     public function getDefaultAddress($param)
     {
         $where['user_id'] = $param['user_id'];
         $where['is_default'] = 1;
         $result = AddressModel::where($where)->field('id,first_name,last_name,email,phone,address,remark,country_id,city_id,country_name,city_name,zip_code,state,state_id,company,address2,remark')->find();
         if (!$result) {
             return [];
         } else {
             return $result->toArray();
         }
     }

    /**
     *  获取地址详情
    */
    public function detail($param)
    {
        $where['user_id'] = $param['user_id'];
        $where['id'] = $param['id'];

        $result = AddressModel::where($where)
            ->field('id,first_name,last_name,email,phone,type,address,is_default,country_id,city_id,country_name,city_name,zip_code,state,state_id,company,address2,remark')
            ->find();

        if( !$result ) {
            return Basic::data(1,'address.addressNotFound');
        }

        return Basic::data(0,'common.success',$result);
    }


    /**
     *  添加编辑地址
    */
    public function save($param)
    {

        /* 获取国家名称 */
        $country = RegionCountry::where('id',$param['country_id'])->find();

        if( !$country ){
            return Basic::data(1,'address.countryIdAlreadyExists');
        }

        $param['country_name'] =  $country->name;
        $param['country_id'] =  $country->id;

        // 城市和州
        if( !empty($param['city_id']) ) {

            $City = RegionCity::where('id',$param['city_id'])->find();

            if( !$City ){
                return Basic::data(1,'address.cityIdAlreadyExists');
            }

            $param['city_id'] =  $City->id;
            $param['city_name'] =  $City->name;
            $param['state'] =  $City->state;
            $param['state_id'] =  $City->state_code;

        }


        if ( empty($param['id']) ) {

            $param['create_time'] = date('Y-m-d H:i:s',time());
            $where['user_id'] = $param['user_id'];
            $where['address'] = $param['address'];
            $where['type'] = $param['type'];

            $result = AddressModel::where($where)->find();

            /* 地址以存在 */
            if( $result ){
                return Basic::data(1,'address.addressAlreadyExists');
            }

            /* 第一次提交 */
            if (!AddressModel::where('user_id',$param['user_id'])->find()) {
                $param['is_default'] = 1;
            }


            $result = AddressModel::create($param);

            if( $result ){

                /* 设置默认 */
                if( $param['is_default'] == 1 ) {
                    $this->addDefault($result);
                }

                return Basic::data(0,'address.savedSuccessfully');
            } else {
                return Basic::data(1,'address.saveFailed');
            }

        } else {

            $where['id'] = $param['id'];
            $where['user_id'] = $param['user_id'];
            $where['type'] = $param['type'];
            $result = AddressModel::where($where)->find();


            /* 地址以存在 */
            if( !$result ){
                return Basic::data(1,'address.addressNotExist');
            }

            $param['update_time'] = date('Y-m-d H:i:s',time());

            $result = AddressModel::update($param);

            if( $result ){

                /* 设置默认 */
                if( $param['is_default'] == 1 ) {
                    $this->addDefault($result);
                }

                return Basic::data(0,'address.updateSuccessfully');
            } else {
                return Basic::data(1,'address.updateFailed');
            }

        }

    }

    /**
     *  添加修改设置默认地址
    */
    public function addDefault($data)
    {
        AddressModel::where('user_id',$data->user_id)->where('is_default',1)->where('type',$data->type)->update([
            'is_default' => 0
        ]);

        AddressModel::where('user_id',$data->user_id)->where('id',$data->id)->update([
            'is_default' => 1
        ]);
    }


    /**
     *  设置默认地址
    */
    public function setDefault($param)
    {
        $where['user_id'] = $param['user_id'];
        $where['id'] = $param['id'];
        $where['type'] = $param['type'];

        $result = AddressModel::where($where)->find();

        if( !$result ) {
            return Basic::data(1,'address.addressNotFound');
        }

        /* 修改默认 */
        AddressModel::where([
            'user_id' => $param['user_id'],
            'type' => $param['type']
        ])
            ->update([
            'is_default' => 0
        ]);

        $result = AddressModel::where($where)->update([
                'is_default' => 1
            ]);

        if( $result ){
            return Basic::data(0,'address.settingSuccessful');
        } else {
            return Basic::data(1,'address.settingFailed');
        }

    }

    /**
     *  设置默认地址
     */
    public function del($param)
    {
        $where['user_id'] = $param['user_id'];
        $where['id'] = $param['id'];


        $result = AddressModel::where($where)->find();

        if( !$result ) {
            return Basic::data(1,'address.addressNotFound');
        }

        if ( $result->is_default == 1 ) {
            return Basic::data(1,'address.addressNotFound');
        }

        $result =  AddressModel::where($where)->delete();

        if( $result ){
            return Basic::data(0,'address.deleteSuccessful');
        } else {
            return Basic::data(1,'address.deleteFailed');
        }

    }




}
